//
//  ChangePassWordVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/11.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "ChangePassWordVC.h"

@interface ChangePassWordVC ()
@property (copy, nonatomic) NSArray *arrTitles;
@property (strong, nonatomic) NSArray *arrContent;
@property (strong, nonatomic) CJButton *btnSave;
@property (nonatomic,strong) CJButton *btnOldEye;
@property (nonatomic,strong) CJButton *btnEye;
@end

@implementation ChangePassWordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitleName:@"修改密码"];
    [self setBackLeftButton:@""];
    [self createUI];
    [self.view addSubview:self.btnSave];
}

- (void)createUI{
    
    for (int i = 0; i < self.arrTitles.count; i++) {
        
        UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(0,kTopHeight + CJToViewMarginH+ i * LW(50), SCREEN_WIDTH, LW(50))];
        liebiaoView.backgroundColor = [UIColor cjWhiteColor];
        [self.view addSubview:liebiaoView];

        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, LW(5), LW(100), LW(40))];
        label.text = self.arrTitles[i];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont cjTitleFont14];
        label.textColor = [UIColor cjMainTextColor];
        [liebiaoView addSubview:label];
        
        UITextField *textField = [UITextField textFieldWithFrame:CGRectMake(label.right, label.top, SCREEN_WIDTH - CJToLeftMargin-label.width - LW(25) - 50, LW(40)) placeholder:self.arrContent[i] backgroundColor:[UIColor cjClearColor] borderColor:nil cornerRadius:0 font:[UIFont cjTitleFont14] textColor:[UIColor cjMainTextColor] textAlignment:NSTextAlignmentLeft text:@"" delegate:nil];
        textField.tag = 1000+ i;
        [liebiaoView addSubview:textField];
        textField.secureTextEntry = YES;
        
        if (@available(iOS 12.0, *)) {
            textField.textContentType = UITextContentTypeOneTimeCode;

        } else {
            // Fallback on earlier versions
        }
    
        UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, liebiaoView.height -1, SCREEN_WIDTH, 1)];
        bg.backgroundColor = [UIColor cjBackgroundColor];
        [liebiaoView addSubview:bg];
        
        if (i == 0) {
            //
            self.btnOldEye = CJButton.btnInit
            .btnFrame(CGRectMake(liebiaoView.width - 50,(liebiaoView.height -10) /2, 18, 10))
            .btnTargetAction(self,@selector(btnHideOrShowOldPWAction:))
            ;
            [liebiaoView addSubview:self.btnOldEye];
            self.btnOldEye.selected = YES;
            [self.btnOldEye setImage:[UIImage imageNamed:@"icon_open_eye"] forState:UIControlStateNormal];
            [self.btnOldEye setImage:[UIImage imageNamed:@"icon_close_eye"] forState:UIControlStateSelected];

        }
        
        if (i == 1) {
            //
            self.btnEye = CJButton.btnInit
            .btnFrame(CGRectMake(liebiaoView.width - 50,(liebiaoView.height -10) /2, 18, 10))
            .btnTargetAction(self,@selector(btnHideOrShowPWAction:))
            ;
            [liebiaoView addSubview:self.btnEye];
            self.btnEye.selected = YES;
            [self.btnEye setImage:[UIImage imageNamed:@"icon_open_eye"] forState:UIControlStateNormal];
            [self.btnEye setImage:[UIImage imageNamed:@"icon_close_eye"] forState:UIControlStateSelected];
        }
    }
    
}
- (void)btnHideOrShowOldPWAction:(UIButton *)sender{
    
    UITextField *filed3 = [self.view viewWithTag:1000];
//    UITextField *filed4 = [self.view viewWithTag:1002];
//    .btnNormalImgName(@"icon_close_eye")
//            .btnSelectImgName(@"icon_open_eye")
    if (self.btnOldEye.selected) {
        filed3.secureTextEntry = NO;
//        filed4.secureTextEntry = NO;
        self.btnOldEye.selected = NO;
        [self.btnOldEye setImage:[UIImage imageNamed:@"icon_open_eye"] forState:UIControlStateNormal];
    }else{
        filed3.secureTextEntry = YES;
//        filed4.secureTextEntry = YES;
        self.btnOldEye.selected = YES;
        [self.btnOldEye setImage:[UIImage imageNamed:@"icon_close_eye"] forState:UIControlStateSelected];
    }
    
}

- (void)btnHideOrShowPWAction:(UIButton *)sender{
    
    UITextField *filed3 = [self.view viewWithTag:1001];
    UITextField *filed4 = [self.view viewWithTag:1002];
//    .btnNormalImgName(@"icon_close_eye")
//            .btnSelectImgName(@"icon_open_eye")
    if (self.btnEye.selected) {
        filed3.secureTextEntry = NO;
        filed4.secureTextEntry = NO;
        self.btnEye.selected = NO;
        [self.btnEye setImage:[UIImage imageNamed:@"icon_open_eye"] forState:UIControlStateNormal];
    }else{
        filed3.secureTextEntry = YES;
        filed4.secureTextEntry = YES;
        self.btnEye.selected = YES;
        [self.btnEye setImage:[UIImage imageNamed:@"icon_close_eye"] forState:UIControlStateSelected];
    }
}

- (void)btnSaveAction{
    UITextField *filed1 = [self.view viewWithTag:1000];
    UITextField *filed2 = [self.view viewWithTag:1001];
    UITextField *filed3 = [self.view viewWithTag:1002];

    if (IsStrEmpty(filed1.text)) {
        [[[AFViewShaker alloc] initWithView:filed1] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"旧密码不能为空"];
            
        }];
    }else if (IsStrEmpty(filed2.text)){
        [[[AFViewShaker alloc] initWithView:filed2] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"新密码不能为空"];
            
        }];
    }else if (IsStrEmpty(filed3.text)){
        [[[AFViewShaker alloc] initWithView:filed3] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"确认密码不能为空"];
            
        }];
    }else if (![filed2.text isEqualToString:filed3.text]){
        [[[AFViewShaker alloc] initWithView:filed2] shakeWithDuration:[ShakerTime floatValue] completion:^{
            [CJAlert showStringDismissWithDelay:@"两次新密码不一致请重新输入"];
            
        }];
        [[[AFViewShaker alloc] initWithView:filed3] shakeWithDuration:[ShakerTime floatValue] completion:^{
                   
        }];
    }else{
            
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/party/updatePass",CJBaseUrl);

        NSDictionary *dicPra = @{@"newPwd":[CJControl base64:filed2.text],
                                 @"oldPwd":[CJControl base64:filed1.text]
                                };
        WS(ws);
        [[HDNetworking sharedHDNetworking] dataTaskWithHTTPMethod:PostType url:strUrl parameters:dicPra status:OperationTip success:^(id  _Nonnull responseObject) {
            
            [ws showLoginWindow];

        } failure:^(NSError * _Nonnull error) {
            
        }];
    }

}

#pragma mark ----------lazy
- (CJButton *)btnSave{
    if (!_btnSave) {
        _btnSave = CJButton.btnInit
        .btnFrame(CGRectMake(2*CJToLeftMargin, (LW(50) + CJToViewMarginH )*self.arrTitles.count + kTopHeight, SCREEN_WIDTH - 4*CJToLeftMargin, LW(50)))
        .btnFont([UIFont cjTitleFont17])
        .btnText(@"修改")
        .btnBgColor([UIColor cjMainColor])
        .btnCornerRadius(5)
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnTargetAction(self, @selector(btnSaveAction))
        ;
    }
    return _btnSave;
}

- (NSArray *)arrContent{
    if (!_arrContent) {
        _arrContent = @[@"请填写旧密码",@"请填写新密码",@"请再输入一次"];
    }
    return _arrContent;
}

- (NSArray *)arrTitles{
    if (!_arrTitles) {
        _arrTitles = @[@"旧密码：",@"新密码:",@"确认密码:"];
    }
    return _arrTitles;
}

@end
